package com.gagakuai.transport.channel;

import java.lang.reflect.Constructor;

/*
 * @description: 反射工厂
 * @author: houhong
 * @create: 2024-03-18 23:49
 */
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {


    private final Constructor<? extends T> constructor;

    public ReflectiveChannelFactory(Class<? extends T> clazz) {

        try {

            this.constructor = clazz.getConstructor();
        } catch (NoSuchMethodException e) {

            throw new RuntimeException(e);
        }
    }

    @Override
    public T newChannel() {

        try {
            return constructor.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
